/******************************************************************************
 * FileName:    gwin_misc_api.h
 * Desc:        杂项接口
 *
 *
 * Author:      Lin
 * Notes:
 *
 ******************************************************************************/

#ifndef __GWIN_MISC_API_H__
#define __GWIN_MISC_API_H__


/*-------------------------- Includes ---------------------------------------*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>


/*-------------------------- Macro Definitions ------------------------------*/

/*-------------------------- Type Declarations ------------------------------*/
#ifndef false
#define false                                   ( 0 )
#endif
#ifndef true
#define true                                    ( !false )
#endif

#ifndef FALSE
#define FALSE                                   ( 0 )
#endif
#ifndef TRUE
#define TRUE                                    ( !FALSE )
#endif

#ifndef NULL
#ifdef __cplusplus
#define NULL                                    ( 0 )
#else
#define NULL                                    ( (void *)0 )
#endif
#endif


#define __TO_STR(a)                             #a
#define _TO_STR(a)                              __TO_STR(a)


#ifndef MIN
#define MIN(a, b)                               ((a) < (b) ? (a) : (b))
#endif

#ifndef MAX
#define MAX(a, b)                               ((a) > (b) ? (a) : (b))
#endif

#ifndef MIN3
#define MIN3(a, b, c)                           MIN(MIN(a, b), c)
#endif

#ifndef MAX3
#define MAX3(a, b, c)                           MAX(MAX(a, b), c)
#endif

#ifndef ABS
#define ABS(a)                                  ((a) < 0 ? (-(a)) : (a))
#endif

#ifndef ABS2
#define ABS2(a, b)                              ((a) > (b) ? ((a) - (b)) : ((b) - (a)))
#endif

#ifndef ABS2_SYMBOL
#define ABS2_SYMBOL(a, b)                       ((a) > (b) ? 1 : -1)
#endif

#ifndef ARRAY_NUM
#define ARRAY_NUM(a)                            (sizeof (a) / sizeof (a[0]))
#endif

#ifndef ESP
#define ESP                                     ( 0.000001f )
#endif

#define FLOAT_EQUAL_ZERO(a)                     ((a) >= -ESP && (a) <= ESP )

#define FLOAT_EQUAL(a, b)                       FLOAT_EQUAL_ZERO((a) - (b))

#define INVALID_I8                              ( 0x7F )
#define INVALID_U8                              ( 0xFF )
#define INVALID_U16                             ( 0xFFFF )
#define INVALID_U32                             ( 0xFFFFFFFF )
#define HL_SWAP_U16(x)                          ((((uint16_t)x << 8) & 0xFF00) | ((uint16_t)x >> 8))
#define HL_SWAP_U32(x)                          ((((uint32_t)x << 24) & 0xFF000000) | (((uint32_t)x << 8)&0x00FF0000) | (((uint32_t)x >> 8) & 0x0000FF00) | (((uint32_t)x >> 24) & 0x000000FF))


//#define BIT(n)                                  (1UL << (n))
#define BIT_GET(a, n)                           ((a) & BIT(n))
#define BIT_SET(a, n)                           ((a) |= BIT(n))
#define BIT_CLR(a, n)                           ((a) &= (~BIT(n)))
#define BIT_XOR(a, n)                           ((a) ^= (~BIT(n)))
#define BIT_CHK_1(a, n)                         (BIT_GET(a, n) == BIT(n))
#define BIT_CHK_0(a, n)                         (BIT_GET(a, n) == 0x00)


/*-------------------------- Global Variables -------------------------------*/

/*-------------------------- Local Variables --------------------------------*/

/*-------------------------- Functions Declarations -------------------------*/

/*-------------------------- Functions Definitions --------------------------*/
#define _os_timer_handle(timer)                 os_timer_handle_##timer
#define _os_timer_declaration(timer)            static void *os_timer_handle_##timer = NULL;                        \
                                                static uint8_t os_timer_is_active_##timer = 0;                      \
                                                static uint8_t os_timer_repeat_##timer = 0;                         \
                                                static uint16_t os_timer_ms_##timer = 0
#define _os_timer_create(timer, period_time, repeat, arg, callback)                                                  \
                                                os_timer_ms_##timer = period_time;                                  \
                                                os_timer_repeat_##timer = repeat;                                   \
                                                miio_timer_create(&os_timer_handle_##timer, callback, (mible_timer_mode)repeat)
#define _os_timer_start(timer, wait_ms)         os_timer_is_active_##timer = 1;                                     \
                                                miio_timer_start(os_timer_handle_##timer, os_timer_ms_##timer, NULL)
#define _os_timer_stop(timer, wait_ms)          os_timer_is_active_##timer = 0;                                     \
                                                miio_timer_stop(os_timer_handle_##timer)
#define _os_timer_delete(timer, wait_ms)        os_timer_is_active_##timer = 0;                                     \
                                                miio_timer_delete(os_timer_handle_##timer)
#define _os_timer_is_active(timer)              (os_timer_is_active_##timer == 1)
#define _os_timer_clear(timer)                  if (!os_timer_repeat_##timer)                                       \
                                                {                                                                   \
                                                    if (os_timer_is_active_##timer)os_timer_is_active_##timer = 0;  \
                                                }
#define os_timer_set_time(timer, preiod_time)   os_timer_ms_##timer = preiod_time;

//#define os_msg_send(task, argv)                 task((void *)argv)

#define system_tick_ms()                        plt_time_read_ms()
//#define network_state()
#define network_restore()                       miio_system_restore()
#define system_reboot()                         miio_system_reboot()
//#define system_set_utc(data)                    miio_system_set_utc(data)
//#define system_get_utc()                        miio_system_get_utc()
#define gap_get_mac(data)                       gap_get_param(GAP_PARAM_BD_ADDR, data)
#define gap_set_adv(data, len)                  gwin_ble_adv_send(data, len)

#define spec_request_utc_time()                 P_128_3_UTC_Time_doRequest()
#define spec_request_gmt_offset()               P_128_1_GMT_Offset_doRequest()
#define spec_request_weather()                  P_128_2_Weather_doRequest()



uint8_t _check_sum(uint8_t *data, uint16_t len);

void hex_str_to_hex(const uint8_t *hex_str, uint32_t hex_str_len, uint8_t *hex, uint32_t hex_len);
void hex_to_hex_str(const uint8_t *hex, uint32_t hex_len, uint8_t *hex_str, uint32_t hex_str_len);

void select_sort(uint16_t *data, uint16_t len);
uint32_t average_pos(uint16_t *data, uint16_t len, uint16_t pos);

// LIN MARK 230519 : MHCWB11S SDK utc接口不能用
uint32_t system_get_utc(void);
void system_set_utc(uint32_t utc_time);


#endif      // __GWIN_MISC_API_H__


/*-------------------------- END OF FILE ------------------------------------*/

